<?php

namespace App\Models\Goods;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Model;

/**
 * 货品规格值模型类
 *
 * @author WangBin
 * @date 2021-09-24
 * @package App\Models\Goods
 */
class SpecValue extends BaseModel
{
    protected $table = 'b2c_goods_spec_value';
    protected $primaryKey = 'value_id';
    public $timestamps = false;
    protected $fillable = [
        'spec_id',
        'value'
    ];

    /**
     * 根据货品ID、规格值查询规格
     *
     * @param int $goodsId
     * @param $specName
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function getSpecByName(int $goodsId, $specName)
    {
        return SpecValue::query()->where('goods_id', $goodsId)->where('value', $specName)->first();
    }

    /**
     * 根据规格ID获取配置的值
     *
     * @param int $specId
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getBySpecId(int $specId)
    {
        return SpecValue::query()->where('spec_id', $specId)->get();
    }

    /**
     * 根据ID获取规格值
     *
     * @param int $valueId
     * @return
     */
    public static function getById(int $valueId)
    {
        return SpecValue::query()->where('value_id', $valueId)->first();
    }

    /**
     * 删除规格的值
     *
     * @param int $specId
     * @return mixed
     */
    public static function removeBySpecId(int $specId)
    {
        return SpecValue::query()->where('spec_id', $specId)->delete();
    }

    /**
     * 根据规格ID和规格值获取规格值信息
     *
     * @param $specId
     * @param $val
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function getBySpecIdAndVal($specId, $val)
    {
        return SpecValue::query()->where('spec_id', $specId)->where('value', $val)->first();
    }

    /**
     * 保存规格值
     *
     * @param int $specId
     * @param array $data
     * @return mixed
     */
    public static function saveData(int $specId, array $data)
    {
        $sum = 0;
        foreach ($data as $value)
        {
            $specValue = SpecValue::query()->where('spec_id', $specId)->where('value', $value)->first();
            if(is_null($specValue)) {
                $specValue = new SpecValue();
            }
            $specValue->spec_id = $specId;
            $specValue->value = $value;
            if($specValue->save()) {
                $sum ++;
            }
        }
        return $sum == count($data);
    }

    /**
     * 根据规格值ID删除
     *
     * @param int $valueId
     * @return mixed
     */
    public static function removeById(int $valueId)
    {
        return SpecValue::query()->where('value_id', $valueId)->delete();
    }

    /**
     * 根据商品 Id 规格值获取规格值 Id
     *
     * @param int $goodsId
     * @param string $specValue
     * @return mixed
     */
    public static function getByGoodsIdAndVal(int $goodsId, string $specValue)
    {
        $specIds = Specs::getByGoodsId($goodsId)->map(function ($item){
            return $item->spec_id;
        });
        return SpecValue::query()->whereIn('spec_id', $specIds)->where('value', $specValue)->first()->value_id;
    }
}
